package com.ikun.yummy.activitys.sign;

import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.view.MenuItem;
import android.view.View;
import android.widget.EditText;
import android.widget.Toast;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;

import com.ikun.yummy.App;
import com.ikun.yummy.R;
import com.ikun.yummy.bean.UserInfo;
import com.ikun.yummy.manager.UserOpManager;
import com.ikun.yummy.utils.CommonUtils;
public class SignActivity extends AppCompatActivity {
    private EditText username,password,password2,email;
    Intent intent=new Intent();
    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_sign);

        initView();
        intent.putExtra("signSuccess",false);
        setResult(2,intent);
        getSupportActionBar().setDisplayHomeAsUpEnabled(true);//左侧添加一个默认的返回图标
        getSupportActionBar().setHomeButtonEnabled(true); //设置返回键可用
    }

    private void initView() {
        username=findViewById(R.id.sign_username);
        password=findViewById(R.id.sign_password);
        password2=findViewById(R.id.sign_password2);
        email=findViewById(R.id.sign_email);
    }

    public void register(View view) {
        if (email.getText().toString().equals("")||username.getText().toString().equals("")||password.getText().toString().equals("")||password2.getText().toString().equals("")){
            Toast.makeText(this,"请输入完整",Toast.LENGTH_SHORT).show();
        }else {
            if (!CommonUtils.isEmail(email.getText().toString())){
                Toast.makeText(this,"您的邮箱有误",Toast.LENGTH_SHORT).show();
                return;
            }
            if (!(password.getText().toString().equals(password2.getText().toString()))){
                Toast.makeText(this,"两次密码不一样",Toast.LENGTH_SHORT).show();
                return;
            }
            UserOpManager userOpManager=new UserOpManager(this);
            if (userOpManager.sign(new UserInfo(username.getText().toString(),password.getText().toString(),"1","",email.getText().toString()))){
                intent.putExtra("signSuccess",true);
                //保存一下下账号密码
                SharedPreferences.Editor edit = App.sp.edit();
                edit.putString("username",username.getText().toString());
                edit.putString("password",password.getText().toString());
                edit.apply();
                setResult(2,intent);
                finish();
            }else{
                Toast.makeText(this,"用户已存在",Toast.LENGTH_SHORT).show();
            }
        }
    }
    @Override
    public boolean onOptionsItemSelected(@NonNull MenuItem item) {
        if (android.R.id.home==item.getItemId()){
            finish();
        }
        return super.onOptionsItemSelected(item);
    }
}
